<%@ page language="java" contentType="text/html; charset=UTF-8"
         import="com.alibaba.fastjson.JSON"
         pageEncoding="UTF-8" %>
<%@ page import="com.baidu.ueditor.ActionEnter" %>
<%@ page import="com.qiniu.storage.model.FileInfo" %>
<%@ page import="common.util.UploadUtil" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.io.FileUtils" %>
<%@ page import="org.apache.commons.io.IOUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="org.springframework.data.redis.core.RedisTemplate" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.*" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%
    request.setCharacterEncoding("utf-8");
    response.setHeader("Content-Type", "text/html");
    String rootPath = application.getRealPath("/");
    String action = request.getParameter("action");
    ServletContext servletContext = request.getServletContext();
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    RedisTemplate redisTemplate = (RedisTemplate) applicationContext.getBean("redisTemplate");

    if (action.equals("listimage")) {
        List<Map> imageList = new ArrayList();
        FileInfo[] items = UploadUtil.listFiles();
        for (FileInfo fileInfo : items) {
            Map image = new HashMap();
            image.put("state", "SUCCESS");
            image.put("url", redisTemplate.opsForHash().get("properties", "qn") + fileInfo.key);
            imageList.add(image);
        }
        Map listimage = new HashMap();
        listimage.put("list", imageList);
        listimage.put("total", imageList.size());
        listimage.put("state", "SUCCESS");
        listimage.put("start", 0);
        String json = JSON.toJSONString(listimage);
        out.write(json);
    } else if (action.equals("uploadimage")) {
        //构造一个文件上传处理对象
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> fileItemList = upload.parseRequest(request);
        for (FileItem fileItem : fileItemList) {
            if (!fileItem.isFormField()) {
                String path = request.getSession().getServletContext().getRealPath("/");
                File file = new File(path, UUID.randomUUID().toString());
                //上传到工程
                FileUtils.copyInputStreamToFile(fileItem.getInputStream(), file);
                IOUtils.closeQuietly(fileItem.getInputStream());
                //上传到云服务器
                UploadUtil.upload(file);
                Map uploadimage = new HashMap();
                uploadimage.put("original", fileItem.getName());
                uploadimage.put("title", file.getName());
                uploadimage.put("size", fileItem.getSize());
                uploadimage.put("type", fileItem.getContentType());
                uploadimage.put("state", "SUCCESS");
                uploadimage.put("url", redisTemplate.opsForHash().get("properties", "qn") + file.getName());
                String json = JSON.toJSONString(uploadimage);
                out.write(json);
            }
        }
    } else {
        out.write(new ActionEnter(request, rootPath).exec());
    }
%>